import { VuexModule, Module, Action, Mutation, getModule } from 'vuex-module-decorators'
import { User } from '@TG/interface/types/user'
import { getToken, setToken, removeToken } from '@TG/kit/cookie'
import { validResponse } from '@TG/kit/request'
import { isNotEmpty } from '@TG/kit/validate'
import store from '@TG/store'
import { appModule } from './app'
import {
  login as loginApi,
  logout as logoutApi,
  sync as syncApi,
  changePassword as changePasswordApi
} from '@/typography/api/auth'

@Module({ dynamic: true, store, name: 'userModule' })
class UserModule extends VuexModule implements User {
  public token = getToken() || ''
  public id = ''
  public name = ''
  public introduction = ''
  public avatar = ''
  public subsystems: string[] = []
  public roles: string[] = []
  public resources: string[] = []
  public meta = {}

  @Mutation
  private SET_TOKEN(token: string) {
    this.token = token
  }

  @Mutation
  private SET_ID(id: string) {
    this.id = id
  }

  @Mutation
  private SET_NAME(name: string) {
    this.name = name
  }

  @Mutation
  private SET_INTRODUCTION(introduction: string) {
    this.introduction = introduction
  }

  @Mutation
  private SET_AVATAR(avatar: string) {
    this.avatar = avatar
  }

  @Mutation
  private SET_SUBSYSTEMS(subsystems: string[]) {
    this.subsystems = subsystems
  }

  @Mutation
  private SET_ROLES(roles: string[]) {
    this.roles = roles
  }

  @Mutation
  private SET_RESOURCES(resources: string[]) {
    this.resources = resources
  }

  @Mutation
  private SET_META(meta: any) {
    this.meta = meta
  }

  @Mutation
  private CLEAR() {
    removeToken()
    this.token = ''
    this.id = ''
    this.name = ''
    this.introduction = ''
    this.avatar = ''
    this.subsystems = []
    this.roles = []
    this.resources = []
    this.meta = {}
  }

  @Action
  public async login(payload: { username: string, password: string }) {
    const username = payload.username.trim()
    const password = payload.password.trim()
    const response = await loginApi(username, password)
    if (validResponse(response)) {
      const token = response.data
      if (isNotEmpty(token)) {
        setToken(token)
        this.SET_TOKEN(token)
      }
    }
  }

  @Action
  public async logout() {
    const response = await logoutApi()
    if (validResponse(response)) {
      this.clear()
    }
  }

  @Action
  public clear() {
    this.CLEAR()
    appModule.clear()
  }

  @Action
  public async sync() {
    const response = await syncApi()
    if (validResponse(response)) {
      const data: User = response.data
      const { subsystems, roles, resources } = data
      this.SET_ID(data.id)
      this.SET_NAME(data.name)
      this.SET_INTRODUCTION(data.introduction)
      this.SET_AVATAR(data.avatar)
      this.SET_SUBSYSTEMS(subsystems)
      this.SET_ROLES(roles)
      this.SET_RESOURCES(resources)
      this.SET_META(data.meta)
      return data
    } else {
      throw Error('无效的请求响应')
    }
  }

  @Action
  public async changePassword(payload: { opwd: string, npwd: string }) {
    const response = await changePasswordApi(payload.opwd, payload.npwd)
    if (validResponse(response)) {
      // -
    }
  }
}

export const userModule = getModule(UserModule)
